博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
15.5. Json 内容展示
阅读量:7097 次
发布时间:2019-06-28

本文共 4019 字,大约阅读时间需要 13 分钟。

Struts 配置文件

information

Action 文件

package cn.netkiller.action;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.StringReader;import java.net.URL;import java.net.URLConnection;import java.nio.charset.Charset;import javax.json.Json;import javax.json.JsonObject;import javax.json.JsonReader;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;public class Infomation extends ActionSupport{	/**	 * 	 */	private static final long serialVersionUID = 1L;	private JsonObject jsonObject = null;	private String jsonString = "";	@Override	public String execute() throws IOException {				String URL = "http://inf.example.com/list/json/93/20/0.html";		System.out.printf("%s Requeted URL is %s", this.getClass().getName(), URL);				StringBuilder sb = new StringBuilder();		URLConnection urlConn = null;		InputStreamReader in = null;		try {			URL url = new URL(URL);			urlConn = url.openConnection();			if (urlConn != null)				urlConn.setReadTimeout(60 * 1000);			if (urlConn != null && urlConn.getInputStream() != null) {				in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());				BufferedReader bufferedReader = new BufferedReader(in);				if (bufferedReader != null) {					int cp;					while ((cp = bufferedReader.read()) != -1) {						sb.append((char) cp);					}					bufferedReader.close();				}			}			in.close();			jsonString = sb.toString();			System.out.println(jsonString);			JsonReader reader = Json.createReader(new StringReader(jsonString));			JsonObject jsonObject = reader.readObject();			this.setJsonObject(jsonObject);			reader.close();			// System.out.println(jsonObject.size());			/*for (int i = 0; i < jsonObject.size() - 2; i++) {				JsonObject rowObject = jsonObject.getJsonObject(Integer.toString(i));				// System.out.println(rowObject.toString());				System.out.printf("%s\t%s\t%s\n", rowObject.getJsonString("id"), rowObject.getJsonString("title"),						rowObject.getJsonString("ctime"));			}*/								} catch (Exception e) {			throw new RuntimeException("Exception while calling URL:" + URL, e);		}		return Action.SUCCESS;	}	public JsonObject getJsonObject() {		return jsonObject;	}	public void setJsonObject(JsonObject jsonObject) {		this.jsonObject = jsonObject;	}	public String getJsonString() {		return jsonString;	}	public void setJsonString(String jsonString) {		this.jsonString = jsonString;	}}

JSP 文件

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
${json.key+1} ${json.value.getJsonString("id")} ${json.value.getJsonString("title")} ${json.value.getJsonString("ctime")}
===================
${json.value.toString()}
===========

解决双引号问题

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
">
首页上一页下一页尾页Count: ${pages.count}, Total: ${pages.total}

15.5.1. 禁止方法

@JSON(serialize = false)	public String getDatas() {		return datas;	}

使用 excludeProperties 在 Action 中排除

true
true
.*direction

15.5.2. 格式化日期

@JSON(format="yyyy-MM-dd") public Date getDate(){ 	return date; }

15.5.3. 重命名变量名

@JSON(name="mypassword") public String getPassword() { 	return password; }

15.5.4. org.apache.struts2.json

JSONUtil.serialize(sb.toString());JSONUtil.deserialize(sb.toString());

原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
转载 - sql分页优化
查看>>
比较好的Dapper封装的仓储实现类 来源:https://www.cnblogs.com/liuchang/articles/4220671.html...
查看>>
2018焦作区域赛E. Resistors in Parallel
查看>>
WebService之Axis2(2):复合类型数据的传递
查看>>
第十一次作业
查看>>
CSVN部署安装,实现web管理svn
查看>>
Methods of Applied Mathematics
查看>>
ie10的input框新特性的去除
查看>>
IOS证书/私钥/代码签名/描述文件
查看>>
二分图的匹配的基础知识
查看>>
html--特殊字符过滤
查看>>
单向链表实现
查看>>
MYSQL连接字符串参数解析(解释)
查看>>
Eratosthenes筛选法
查看>>
HDU1008 ZOJ2108 Elevator
查看>>
2013 ACM/ICPC Asia Regional Chengdu Online hdu4731 Minimum palindrome
查看>>
LINQ~什么时候使用SelectMany和GroupBy
查看>>
js 遇到 Permission denied to access property ***
查看>>
杭电1509--Windows Message Queue(优先队列)
查看>>
C#中文转换成拼音
查看>>